﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Service4u2.Common;

namespace MoAds.AdAdapters.MillennialMedia
{
    public class MillennialMediaAdapter : AdAdapter
    {
        private MillennialMediaService mmService;

        /// <summary>
        /// Gets or sets the meta data.
        /// </summary>
        /// <value>Meta Data.</value>
        public string Mode
        { get; set; }

        /// <summary>
        /// Gets or sets the client id.
        /// </summary>
        /// <value>Client Id.</value>
        public string PlacementId
        { get; set; }

        public MillennialMediaAdapter()
        {
            mmService = new MillennialMediaService();

            mmService.ResultEvent += new EventHandler<EventArgs<MillennialMediaResponse>>(OnResult);
            mmService.ExceptionEvent += new EventHandler<EventArgs<Exception>>(OnException);
        }

        public override void BeginAdRequestAsync()
        {
            MillennialMediaRequest request = new MillennialMediaRequest();
            request.IPAddress = "128.0.0.1"; // can't get the IPAddress in WP7
            request.Mode = Mode;
            request.UserAgent = BrowserAgentString;
            request.PlacementId = PlacementId;
            request.UniqueId = DeviceUniqueId;

            mmService.BeginAdsAsync(request);
        }

        protected virtual void OnResult(object sender, EventArgs<MillennialMediaResponse> e)
        {
            AdResponse response = new AdResponse()
            {
                ClickUrl = e.Argument.ClickUri, 
                ImageUrl = e.Argument.ImageUri
            };

            OnResult(response);
        }
    }
}
